package cn.iocoder.yudao.module.erp.controller.admin.business.group.suppliergroup;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.group.suppliergroup.vo.SupplierGroupPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.group.suppliergroup.vo.SupplierGroupRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.group.suppliergroup.vo.SupplierGroupSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.group.suppliergroup.SupplierGroupDO;
import cn.iocoder.yudao.module.erp.service.business.group.suppliergroup.SupplierGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP供应商分组")
@RestController
@RequestMapping("/erp/supplier-group")
@Validated
public class SupplierGroupController {

    @Resource
    private SupplierGroupService supplierGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商分组")
    @PreAuthorize("@ss.hasPermission('erp:supplier-group:create')")
    public CommonResult<Long> createSupplierGroup(@Valid @RequestBody SupplierGroupSaveReqVO createReqVO) {
        return success(supplierGroupService.createSupplierGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商分组")
    @PreAuthorize("@ss.hasPermission('erp:supplier-group:update')")
    public CommonResult<Boolean> updateSupplierGroup(@Valid @RequestBody SupplierGroupSaveReqVO updateReqVO) {
        supplierGroupService.updateSupplierGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商分组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:supplier-group:delete')")
    public CommonResult<Boolean> deleteSupplierGroup(@RequestParam("id") Long id) {
        supplierGroupService.deleteSupplierGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:supplier-group:query')")
    public CommonResult<SupplierGroupRespVO> getSupplierGroup(@RequestParam("id") Long id) {
        SupplierGroupDO supplierGroup = supplierGroupService.getSupplierGroup(id);
        return success(BeanUtils.toBean(supplierGroup, SupplierGroupRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得供应商精简列表", description = "只包含被开启的产品，主要用于前端的下拉选项")
    public CommonResult<List<SupplierGroupRespVO>> getSupplierSimpleList() {
        List<SupplierGroupDO> supplierList = supplierGroupService.getSupplierList();
        return success(BeanUtils.toBean(supplierList, SupplierGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商分组分页")
    @PreAuthorize("@ss.hasPermission('erp:supplier-group:query')")
    public CommonResult<PageResult<SupplierGroupRespVO>> getSupplierGroupPage(@Valid SupplierGroupPageReqVO pageReqVO) {
        PageResult<SupplierGroupDO> pageResult = supplierGroupService.getSupplierGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplierGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商分组 Excel")
    @PreAuthorize("@ss.hasPermission('erp:supplier-group:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSupplierGroupExcel(@Valid SupplierGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplierGroupDO> list = supplierGroupService.getSupplierGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商分组.xls", "数据", SupplierGroupRespVO.class,
                        BeanUtils.toBean(list, SupplierGroupRespVO.class));
    }

}